package city.spring.modules.work.controller;

import city.spring.modules.work.entity.WorkPlanEntity;
import city.spring.modules.work.service.WorkPlanService;
import city.spring.utils.MyBatisUtils;
import city.spring.utils.OrganizationUtils;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Controller：排班方案（上班方案）
 *
 * @author 侯坤林
 * @date 2020-06-04 16:43:43
 */
@RestController
@RequestMapping("/work_plan")
public class WorkPlanController extends ApiController {
    private final WorkPlanService workPlanService;
    /**
     * 实体类可排序字段
     */
    private final List<SFunction<WorkPlanEntity, ?>> entityOrderFields;

    public WorkPlanController(WorkPlanService workPlanService) {
        this.workPlanService = workPlanService;
        entityOrderFields = new ArrayList<>();
        entityOrderFields.add(WorkPlanEntity::getId);
        entityOrderFields.add(WorkPlanEntity::getTitle);
        entityOrderFields.add(WorkPlanEntity::getWorkingDay);
        entityOrderFields.add(WorkPlanEntity::getVersion);
        entityOrderFields.add(WorkPlanEntity::getGmtCreated);
        entityOrderFields.add(WorkPlanEntity::getGmtModified);
    }

    /**
     * 获取全部的 <strong>排班方案（上班方案）</strong> 列表
     */
    @GetMapping("all")
    public Object listAll(@PageableDefault(sort = {"gmtCreate"}) Pageable pageable, WorkPlanEntity entity) {
        LambdaQueryChainWrapper<WorkPlanEntity> lambdaQuery = buildLambdaQuery(entity);
        OrganizationUtils.includes(lambdaQuery, WorkPlanEntity::getOrgId);
        MyBatisUtils.lambdaQueryAddOrder(lambdaQuery, pageable, entityOrderFields);
        return success(lambdaQuery.list());
    }

    /**
     * 分页获取 <strong>排班方案（上班方案）</strong> 列表
     *
     * @param pageable 分页参数信息
     */
    @GetMapping
    public Object list(@PageableDefault(sort = {"gmtCreate"}) Pageable pageable, WorkPlanEntity entity) {
        LambdaQueryChainWrapper<WorkPlanEntity> lambdaQuery = buildLambdaQuery(entity);
        OrganizationUtils.includes(lambdaQuery, WorkPlanEntity::getOrgId);
        MyBatisUtils.lambdaQueryAddOrder(lambdaQuery, pageable, entityOrderFields);
        Page<WorkPlanEntity> page = lambdaQuery.page(MyBatisUtils.toPage(pageable, false));
        return success(page);
    }

    /**
     * 获取一个 <strong>排班方案（上班方案）</strong>
     *
     * @param id 主键ID
     */
    @GetMapping("{id}")
    public Object info(@PathVariable String id) {
        return success(workPlanService.getWorkPlan(id));
    }

    /**
     * 添加一个 <strong>排班方案（上班方案）</strong>
     *
     * @param entity 修改后的信息
     */
    @PostMapping
    public Object add(@RequestBody WorkPlanEntity entity) {
        workPlanService.saveWorkPlan(entity);

        HttpHeaders headers = new HttpHeaders();
        headers.add(HttpHeaders.LOCATION, String.valueOf(entity.getId()));
        return new ResponseEntity<>(entity, headers, HttpStatus.CREATED);
    }

    /**
     * 修改一个 <strong>排班方案（上班方案）</strong>
     *
     * @param id     主键ID
     * @param entity 修改后的信息
     */
    @PutMapping("{id}")
    public Object update(@PathVariable String id, @RequestBody WorkPlanEntity entity) {
        entity.setId(id);
        workPlanService.updateWorkPlan(entity);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    /**
     * 删除一个 <strong>排班方案（上班方案）</strong>
     *
     * @param id 主键ID
     */
    @DeleteMapping("{id}")
    public Object delete(@PathVariable String id) {
        workPlanService.deleteWorkPlan(id);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 删除多个 <strong>排班方案（上班方案）</strong>
     *
     * @param ids 主键ID列表
     */
    @DeleteMapping
    public Object deleteIds(@RequestBody List<String> ids) {
        workPlanService.deleteWorkPlan(ids);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 构建查询条件内容
     *
     * @param entity 实体对象
     * @return lambda query chain wrapper
     */
    private LambdaQueryChainWrapper<WorkPlanEntity> buildLambdaQuery(WorkPlanEntity entity) {
        LambdaQueryChainWrapper<WorkPlanEntity> lambdaQuery = workPlanService.lambdaQuery();
        lambdaQuery.eq(StringUtils.isNotBlank(entity.getId()), WorkPlanEntity::getId, entity.getId());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getTitle()), WorkPlanEntity::getTitle, entity.getTitle());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getWorkingDay()), WorkPlanEntity::getWorkingDay, entity.getWorkingDay());
        lambdaQuery.eq(entity.getVersion() != null, WorkPlanEntity::getVersion, entity.getVersion());
        lambdaQuery.eq(entity.getGmtCreated() != null, WorkPlanEntity::getGmtCreated, entity.getGmtCreated());
        lambdaQuery.eq(entity.getGmtModified() != null, WorkPlanEntity::getGmtModified, entity.getGmtModified());
        return lambdaQuery;
    }

}
